package com.hospital.doctor.doctorh.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.hospital.doctor.doctorh.entity.*;
import com.hospital.doctor.doctorh.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/doctorh")
public class DoctorhController {

    @Autowired
    DoctorService doctorService;

    @RequestMapping("/index")
    String index(Model model, Integer doctorId, @RequestParam(defaultValue = "1") Integer start, @RequestParam(defaultValue = "5") Integer pageSize) {
        List<PatientInfo> patientInfos = doctorService.selPat(doctorId, start, pageSize);
        PageInfo<PatientInfo> page = new PageInfo<>(patientInfos);
        model.addAttribute("page", page);
        return "doctor/prescribe";
    }

    @RequestMapping("/showCt")
    @ResponseBody
    String showCt(Integer ctNo) {
        List<CtInfo> ctInfos = doctorService.selCt(ctNo);
        if (ctInfos.size() == 0) {
            return JSON.toJSONString("false");
        }
        return JSON.toJSONString(ctInfos);
    }

    @RequestMapping("/showHis")
    @ResponseBody
    String showHis(Integer ctNo) {
        boolean b = doctorService.selCtCount(ctNo);
        if (b) {
            List<DiseaseInfo> diseaseInfos = doctorService.selDisease(ctNo);
            return JSON.toJSONString(diseaseInfos);
        }
        return JSON.toJSONString("false");
    }

    @RequestMapping("/kaiyao")
    @ResponseBody
    String kaiYao(Integer id, String name, Integer doctorId, String time, String drugid, String drugsum, String drugtype, String bingli) {
        //去掉药品id drugid 数量 drugsum 医嘱 drugtype中的-，分割成数组
        String[] drugids = drugid.split("-");
        String[] drugsums = drugsum.split("-");
        String[] drugtypes = drugtype.split("-");
        //创建List<Medicine>集合，保存Medicine对象
        List<Medicine> insertlist = new ArrayList<>();
        Medicine m = new Medicine();
        m.setUserid(id);
        m.setUsername(name);
        m.setMedicineid(Integer.parseInt(drugids[0]));
        m.setSum(Integer.parseInt(drugsums[0]));
        m.setTime(time);
        m.setDoctorid(doctorId);
        m.setType(drugtypes[0]);
        insertlist.add(m);
        boolean b = doctorService.insertList(insertlist, bingli);
        return JSON.toJSONString(b);
    }

    @RequestMapping("/drug")
    @ResponseBody
    Object drug() {
        List<Drugs> list = doctorService.list();

        List<Dr> dr = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            dr.add(
                    new Dr(
                            list.get(i).getId(),
                            list.get(i).getDrugname()
                    ));
        }
        return JSON.toJSON(dr);
    }

    @RequestMapping("/inspect")
    @ResponseBody
    String inspect(Integer id) {
        boolean checkinsert = doctorService.checkinsert(id);
        return JSON.toJSONString(checkinsert);
    }

}
